package ai.people.netmon.warning.service.impl;

import ai.people.netmon.framework.domain.warning.entity.WarningAccountCollect;
import ai.people.netmon.framework.domain.warning.entity.WarningInfoCollect;
import ai.people.netmon.warning.mapper.WarningAccountCollectMapper;
import ai.people.netmon.warning.service.WarningAccountCollectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author ligua
* @description 针对表【warning_account_collect(预警账号收藏表)】的数据库操作Service实现
* @createDate 2022-07-28 17:50:38
*/
@Service
@RequiredArgsConstructor
public class WarningAccountCollectServiceImpl extends ServiceImpl<WarningAccountCollectMapper, WarningAccountCollect>
    implements WarningAccountCollectService{

    private final WarningAccountCollectMapper warningAccountCollectMapper;

    @Resource
    private RedissonClient redissonClient;

    /**
     * 通过参数集合批量查询信息
     *
     * @param checkParamList 账号id
     * @return {@link List}<{@link WarningAccountCollect}>
     */
    @Override
    public List<WarningAccountCollect> batchQueryByParamList(List<WarningAccountCollect> checkParamList) {
        List<WarningAccountCollect> result = Lists.newArrayList();
        if(CollectionUtils.isEmpty(checkParamList)){
            return Lists.newArrayList();
        }
        List<List<WarningAccountCollect>> partition = Lists.partition(checkParamList, 200);
        partition.forEach(list-> result.addAll(warningAccountCollectMapper.selectByParamList(list)));
        return result;
    }

    /**
     * 账户收藏
     *
     * @param accountCollect 账户收取
     * @return {@link Integer}
     */
    @Override
    public Integer accountCollect(WarningAccountCollect accountCollect, String rowDataJson) {
        RLock lock = redissonClient.getLock("warning:account:collect:" + accountCollect.getCollectUserId());
        try {
            if(lock.tryLock(6, TimeUnit.SECONDS)){
                WarningAccountCollect one = getOneByUidAccountIdAndPlatform(accountCollect);
                if(one == null){
                    return warningAccountCollectMapper.insert(accountCollect);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
        return 0;
    }

    /**
     * 查询单个 根据uid和accountId和信息平台
     *
     * @param accountCollect 账号收集
     * @return {@link WarningInfoCollect}
     */
    private WarningAccountCollect getOneByUidAccountIdAndPlatform(WarningAccountCollect accountCollect) {
        return warningAccountCollectMapper.selectOne(new LambdaQueryWrapper<WarningAccountCollect>()
                .eq(Objects.nonNull(accountCollect.getCollectUserId()), WarningAccountCollect::getCollectUserId, accountCollect.getCollectUserId())
                .eq(Objects.nonNull(accountCollect.getCollectAccountId()), WarningAccountCollect::getCollectAccountId, accountCollect.getCollectAccountId())
                .eq(Objects.nonNull(accountCollect.getCollectAccountPlatform()), WarningAccountCollect::getCollectAccountPlatform, accountCollect.getCollectAccountPlatform())
                .eq(WarningAccountCollect::getIsDelete, 0));
    }

    /**
     * 账户取消收藏
     *
     * @param accountCollect 账户收取
     * @return {@link Integer}
     */
    @Override
    public Integer accountCancelCollect(WarningAccountCollect accountCollect) {
        RLock lock = redissonClient.getLock("warning:account:collect:" + accountCollect.getCollectUserId());
        try {
            if(lock.tryLock(6, TimeUnit.SECONDS)){
                WarningAccountCollect one = getOneByUidAccountIdAndPlatform(accountCollect);
                if(one != null){
                    return warningAccountCollectMapper.updateById(WarningAccountCollect.builder().id(one.getId()).isDelete(1).build());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
        return 0;
    }
}




